//
//  SettingsFooter.swift
//  DSP
//
//  Created by tucici on 2024/5/22.
//

import UIKit
import TCCKit
class SettingsFooter: UIView {
    private var tableView:UITableView!
    private var line : UIView!
    var block : ((_ text:String)->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI() {
        
        line = UIView()
        line.bgHexColor = "#000000"
        
        tableView = UITableView(frame:.zero, style:.plain)
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.tableHeaderView = UIView(frame:CGRect(x: 0, y: 0, width: 0, height: 0.01))
        tableView.tableFooterView = UIView(frame:CGRect(x: 0, y: 0, width: 0, height: 0.01))
        tableView.backgroundColor = "#FFFFFF".color()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.ID())
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {}
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        } else { }
        
        addSubview(line)
        addSubview(tableView)
        line.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(0)
            make.left.right.equalToSuperview().inset(10)
            make.height.equalTo(1)
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom)
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0)
        }
    }
    var array = [String](){
        didSet{
            tableView.snp.updateConstraints { make in
                make.height.equalTo(array.count * 42)
            }
            tableView.reloadData()
        }
    }
}
//MARK: - UITableViewDataSource
extension SettingsFooter:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.ID())!
        cell.backgroundColor = .clear
        cell.contentView.removeAllSubView()
        cell.selectionStyle = .none
        
        let name = UILabel()
        name.text = array[indexPath.row]
        name.font = font(18)
        name.textHexColor = "#000000"
        
        let line = UIView()
        line.isHidden = array.count - 1 == indexPath.row
        line.bgHexColor = "#000000"
        
        cell.contentView.addSubview(name)
        cell.contentView.addSubview(line)
        
        name.snp.makeConstraints { make in
            make.height.equalTo(22)
            make.top.bottom.equalToSuperview().inset(10)
            make.centerX.equalToSuperview()
        }
        
        line.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(0)
            make.left.right.equalToSuperview().inset(10)
            make.height.equalTo(1)
        }
        return cell
    }
}
//MARK: - UITableViewDelegate
extension SettingsFooter:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        block?(array[indexPath.row])
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
